package Concurrency2.Chapter05_Lock;

import java.util.concurrent.locks.LockSupport;

/**
 * Instruction：<p>
 * (1) Thread.sleep() 是否释放锁
 * (2) LockSupport.park() 是否释放锁
 * Author：@author MaLi
 */
public class T04_API {
    private static volatile boolean flag = false;

    public static void main(String[] args) {
        final Object lock = new Object();
        Thread t1 = new Thread(() -> {
            //获取到锁
            synchronized (lock) {
                /*System.err.println("获取到锁");
                try {
                    Thread.sleep(2000);
                    lock.wait();
                    System.err.println("继续执行, 准备更新flag为true");
                    flag = true;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }*/
                LockSupport.park(1000); //这里不释放锁
                System.err.println("继续执行, 准备更新flag为true");
                flag = true;
            }
            System.err.println("释放锁完成");
        });
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t2获取到锁, flag已经被赋值完成:  " + flag);
                //lock.notify();
                LockSupport.unpark(t1);
            }
        });

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
